﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Entity;
using yunbo.Model;
using yunbo.Model.SSM;
using yunbo.Repositorys.SSMRepositorys;

namespace yunbo.Controllers.SSMControllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        [HttpGet("UsersRoleID")]
        public IActionResult GetUsersRoleID()
        {
            // 这里您可以根据数据库逻辑返回相应的用户ID和角色ID  
            var result = new { userID = 1, roleID = "Admin" }; // 示例数据  
            return Ok(result);
        }/////////////
        UserRepositorys userRepositorys = new UserRepositorys();
        [HttpGet("DenLu")]
        public IActionResult DenLu(string Name, string Pwd)
        {
            User user = userRepositorys.Select(Name, Pwd);
            return Ok(user);
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetUser")]
        public IActionResult GetUser()
        {
            List<User> user = userRepositorys.GetUser();
            return Ok(user);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost("PostUser")]
        public IActionResult PostUser(TJUser user1)
        {
            //// 获取当前时间，并格式化为字符串
            //string currentTime = DateTime.Now.ToString("yyyyMMdd");

            //// 生成一个四位随机数
            //Random random = new Random();
            //int randomNumber = random.Next(1000, 10000); // 生成1000到9999之间的随机数
            //string randomNumberString = randomNumber.ToString("D4"); // 格式化为四位数字字符串

            //// 组合当前时间和随机数生成ID
            //string id = $"yb{currentTime}{randomNumberString}";
            User user = new User()
            {
                user_job_code = user1.user_job_code,
                user_uname = user1.user_uname,
                user_name = user1.user_name,
                user_email = user1.user_email,
                user_create_by = user1.user_create_by,
                user_phone = user1.user_phone,
                user_create_time = DateTime.Now,
                user_state = 1,
                user_pwd = user1.user_pwd
            };
            bool rs = userRepositorys.PostUser(user) > 0;
            return Ok(rs);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpDelete("DeleteUser")]
        public IActionResult DeleteUser(int id)
        {
            bool rs = userRepositorys.DeleteUser(id) > 0;
            return Ok(rs);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetFYUser")]
        public IActionResult GetFYUser(int PageNumber, int PageSize)
        {
            FYUser user = userRepositorys.GetFYUser(PageNumber, PageSize);
            return Ok(user);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        [HttpPut("UpdateUser")]
        public IActionResult UpdateUser(XGUser xguser)
        {
            User user = new User()
            {
                user_id = xguser.user_id,
                user_update_time = DateTime.Now,
                user_update_by = xguser.user_update_by,
                user_email = xguser.user_email,
                user_phone = xguser.user_phone,
                user_name = xguser.user_name,
                user_pwd = xguser.user_pwd,
                user_uname = xguser.user_uname
            };
            bool i = userRepositorys.UpdateUser(user) > 0;
            return Ok(i);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetFYMHUser")]
        public IActionResult GetFYMHUser(int PageNumber, int PageSize, int? state, string? uname)
        {
            FYUser user = userRepositorys.GetFYMHUser(PageNumber, PageSize, state, uname);
            return Ok(user);
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <returns></returns>
        [HttpPut("UpdateUserZT")]
        public IActionResult UpdateUserZT(string code,int state)
        {
            bool i = userRepositorys.UpdateUserZT(code,state) > 0;
            return Ok(i);
        }
    }
}
